package controller;

import java.sql.SQLException;
import java.util.ArrayList;

import factory.ObserverManager;

import processes.Machine;
import processes.MachineManager;

import DAO.ProductDAO;

import to.OrderTO;
import to.ProductTO;

import manager.MesManager;

public class MesService {
	private ProductDAO dao;
	public MesService(ProductDAO dao){
		this.dao = dao;
	}
	//오더 조회
	public ArrayList<OrderTO> getOrderList() throws SQLException{
		ArrayList<OrderTO> orderList = dao.selectOrderList();
		return orderList;
	}
	//기계별 작동 현황 조회
	public ArrayList<OrderTO> getWorkerList() throws SQLException{
		MesManager manager = new MesManager();
		ArrayList<OrderTO> list = new ArrayList<OrderTO>();
		//받은 오더의 작업 현황 조회
		ArrayList<MachineManager> managerList = ObserverManager.getManagerList();
		for(int i=0; i<managerList.size(); i++){
			ArrayList<Machine> machineList = managerList.get(i).getMachineList();
			for(int j=0; j<machineList.size(); j++){
				Machine mch = machineList.get(j);
				long workerTime = manager.setResultTime(mch.getStartTime());
				if(workerTime > 7){
					list.add(new OrderTO(mch.getLineName()+j, 0));
				}else{
					list.add(new OrderTO(mch.getLineName()+j, 1));
				}
			}
		}
		return list;
	}
	//검수 결과 조회
	public MesManager getOutcomeList(int orderId) throws SQLException{
		ArrayList<ProductTO> productList = dao.selectProductByOrderId(orderId);
		ProductTO pto = new ProductTO();
		MesManager manager = new MesManager();
		for(int i=0; i<productList.size(); i++){
			pto = productList.get(i);
			manager.setAllCount(i);
			if(pto.getOutcome().isPassed()==true){
				manager.sumSuccessCount();
				manager.sumSuccessTime(pto.getCuttingTime()+pto.getDyeingTime()+pto.getSewingTime()+pto.getPressingTime());
			}
		}
		manager.resultSuccess();
		return manager;
	}
}
